﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TetrisV1._0
{
    public partial class ScoreForm : Form
    {
        //声明新窗体
        private Form anotherForm;
        public ScoreForm()
        {
            InitializeComponent();
        }

        public ScoreForm(Form f)
        {
            this.Owner = f;
            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(this.Owner.Location.X, this.Owner.Location.Y);
            InitializeComponent();
            DisplaySortScore();
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            anotherForm = new MainForm(this);//定义新窗体
            this.Hide();//隐藏旧窗体
            anotherForm.ShowDialog();//展示新窗体
            Application.ExitThread();//退出当前线程上的消息循环，并关闭该线程上的所有窗口。
        }

        public void DisplaySortScore()
        {
            string sql = @"SELECT * FROM score ORDER BY score DESC LIMIT 5;";
            MySqlConnection conn = DBControl.GetConnection();
            MySqlCommand cmd = new MySqlCommand(sql, conn);
            MySqlDataReader reader = cmd.ExecuteReader();//执行ExecuteReader()返回一个MySqlDataReader对象
            string[] sortedScore = new string[5] { "0","0", "0", "0" ,"0"};
            string[] dataTime = new string[5] { "0", "0", "0", "0", "0" };
            int i = 0;
            while (reader.Read())//初始索引是-1，执行读取下一行数据，返回值是bool
            {
                sortedScore[i] = reader.GetString("score");
                dataTime[i] = reader.GetDateTime("createtime").ToString();
                i++;
            }
            labelScore1.Text = sortedScore[0];
            labelScore2.Text = sortedScore[1];
            labelScore3.Text = sortedScore[2];
            labelScore4.Text = sortedScore[3];
            labelScore5.Text = sortedScore[4];

            labelTime1.Text = dataTime[0];
            labelTime2.Text = dataTime[1];
            labelTime3.Text = dataTime[2];
            labelTime4.Text = dataTime[3];
            labelTime5.Text = dataTime[4];
            
            reader.Close();

        }
    }
}
